(function($) {

    var corpo       = '#main';  // div corpo ajax
    caracter        = '#!/';    // url caracter ajax
    var controller  = 'home';   // controller inicial
    var action      = 'index';  // action inicial
    var login_base  = ['/']; // base para modulos


    var app = $.sammy(corpo, function() {

        this.helpers({
            efeito_corpo: function(tipo) {
                this.swap = function(content) {
                    switch(tipo) {
                        case 0:
                            this.$element().html(content);
                            break;
                        case 1:
                            this.$element().hide().html(content).fadeIn(1500);
                            break;
                        case 2:
                            this.$element().hide().html(content).show("slide", {
                                direction: "rigth"
                            }, 1500);
                            break;
                    }
                    transformar_url(corpo);
                }
            },

            url_ajax: function(url, params) {
                this.efeito_corpo(1);
                this.partial(url, params);
            },

            post_ajax: function(ctx) {
                $.ajax({
                    type: 'POST',
                    url: ctx.params.controller + "/" + ctx.params.action,
                    data: ctx.params.toHash(),
                    success: function(retorno){
                    }
                });
            },

            post_ajax_teste: function(ctx) {
                $.ajax({
                    type: 'POST',
                    url: ctx.params.controller + "/" + ctx.params.action,
                    data: ctx.params.toHash(),
                    success: function(retorno){
                        alert(retorno);
                        app.clearTemplateCache();
                        app.refresh();
                    }
                });
            }

        });

        // GET #!/
        this.get(caracter, function() {
            this.url_ajax(controller + "/" + action);
        });
        
        // GET #!/folder/id/{id}./\/folder\/id\/(.*)/
        this.get(caracter + 'folder/id/:number', function() {
            $( "#dialog" ).dialog( "open" );
            this.url_ajax("ajax/getdocuments/id/" + this.params.number);
        });
        
        // GET #!/folder/id/{id}./\/folder\/id\/(.*)/
        this.get(caracter + 'folder/id/:number/document/:value', function() {
            $( "#dialog" ).dialog( "open" );
            this.url_ajax("ajax/getdocuments/id/" + this.params.number + "/document/" + this.params.value);
        });
        
        // GET #!/busca
        this.get(caracter + 'busca', function() {
            this.url_ajax("busca/index");
        });

        // GET #!/:action
        this.get(caracter + ':action', function() {
            this.url_ajax(controller + "/" + this.params.action);
        });

        // GET #!/:action/id/:value
        this.get(caracter + ':action/id/:value', function() {
            this.url_ajax(controller + "/" + this.params.action + "/id/" + this.params.value);
        });

        // GET #!/:action/id/:value/:param/:value
        this.get(caracter + ':action/id/:value/:param/:val', function() {
            this.url_ajax(controller + "/" + this.params.action + "/id/" + this.params.value + "/" + this.params.param + "/" + this.params.val);
        });

        // GET #!/:controller/:action
        this.get(caracter + ':controller/:action', function() {
            this.url_ajax(this.params.controller + "/" + this.params.action);
        });

        // GET #!/:controller/:action/:id
        this.get(caracter + ':controller/:action/:id', function() {
            this.url_ajax(this.params.controller + "/" + this.params.action + "/id/" + this.params.id);
        });

        // GET #!/:controller/:action/:id/:param/:value
        this.get(caracter + ':controller/:action/:id/:param/:value', function() {
            this.url_ajax(this.params.controller + "/" + this.params.action + "/id/" + this.params.id + "/" + this.params.param + "/" + this.params.value);
        });

        // POST #!/home/:action
        this.post(caracter + ':action', function(ctx) {
            ctx.params.controller = controller;
            this.post_ajax_teste(ctx);
        });

        // POST #!/:controller/:action
        this.post(caracter + ':controller/:action', function(ctx) {
            this.post_ajax(ctx);
        });
    });

    // inicializa o sammyjs
    $(function() {
        if (!is_login()) {
            correct_base_login();
            app.run(caracter);
            transformar_url("#menu,#footer,#submenu");
        }
    });

    // verifica se a url do modulo está correta
    function correct_base_login(){
        for (var i in login_base)
            if(app.getLocation() == ('/' + login_base[i]))
                app.setLocation('/' + login_base[i] + '/');
    }

    // verifica se é url de login
    function is_login(){
        return (app.getLocation().indexOf("/login") != -1) ? true : false;
    }

    // transforma url para o modo do sammyjs
    function transformar_url(div) {
        $("a[ajax='\u2713'], form[ajax='\u2713']", div).each( function(){
            var href_or_action = $(this).is('a') ? 'href' : 'action';
            var url = $(this).attr(href_or_action);
            if(url && !(url.indexOf(caracter) != -1)) {
                var v = url.split("/");
                v.shift();
                if(v[0] == controller && v[1] == action)  $(this).attr(href_or_action,caracter);
                else if(v[0] == controller) {
                    v.shift();
                    $(this).attr(href_or_action,caracter + v.join("/"));
                }
                else $(this).attr(href_or_action,caracter + v.join("/"));

            }
        });
    }

})(jQuery);

// animacao para alert topo
$(function(){
    var obj = $(".message");

    if (obj.html() && obj.html().trim() !== ""){
        obj.fadeOut(700).fadeIn(1000).fadeOut(700).fadeIn(1000);
        setTimeout( function() {
            obj.slideUp(1000)
        },4000);
    }
});

// formato data dinamica
$(function(){
    var data = new Date();
    $('#hora').countdown({
        since: "24-"+data.getHours()+"h : 60-"+data.getMinutes()+"m : "+data.getSeconds()+"s",
        compact: true,
        description: ''
    });
});

$(function(){
    $("#breadcrumbs a, #menu ul li ul li a").live("click",function(){
        $("ul.pastas ul li").each(function(){
            if ($(this).attr('class') == "directory expanded")
                $(this).find('a').triggerHandler('click');
        });
    });
});

//$(function(){
//    $("#menu").resizable({
//        maxWidth: 350,
//        minWidth: 200
//    });
//});